package com.cz.auth.mapper;

import com.cz.auth.entity.Permission;
import com.cz.auth.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>用户mapper</p>
 * <p>
 * <pre>
 * @author gitamacai
 * @date 2019/11/14 10:11
 * </pre>
 */
public interface UserMapper {
    /**
     * 查询用户信息
     *
     * @param userName 用户名
     * @return 用户信息
     */
    @Select(" select * from sys_user where username = #{userName}")
    User findByUsername(@Param("userName") String userName);

    /**
     * 查询所有的用户
     *
     * @return 返回所有的用户
     */
    @Select("select * from sys_user")
    List<User> getAllUser();

    /**
     * 查询用户的权限
     *
     * @param userName 用户名
     * @return 查询用户的权限
     */
    @Select(" select permission.* from sys_user user" + " inner join sys_user_role user_role"
            + " on user.id = user_role.user_id inner join "
            + "sys_role_permission role_permission on user_role.role_id = role_permission.role_id "
            +
            " inner join sys_permission permission on role_permission.perm_id = permission.id where user.username = #{userName};")
    List<Permission> findPermissionByUsername(@Param("userName") String userName);

    /**
     * 插入用户
     *
     * @param user 用户对象
     * @return 影响行数
     */
    @Insert("insert into sys_user(id, username, realname, password, createDate, lastLoginTime, enabled, accountNonExpired, accountNonLocked, credentialsNonExpired) " +
            "values(#{id}, #{username}, #{realname}, #{password}, #{createDate}, #{lastLoginTime}, #{enabled}, #{accountNonExpired}, #{accountNonLocked}, #{credentialsNonExpired})")
    int insert(User user);

    /**
     * 获取最大id
     *
     * @return 返回user表中最大id
     */
    @Select("select max(id) from sys_user")
    int getMaxId();
}
